package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.SystemDictionary;
import cn.wolfcode.crm.domain.SystemDictionaryitem;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.qo.SystemDictionaryitemQueryObiect;
import cn.wolfcode.crm.service.SystemDictionaryService;
import cn.wolfcode.crm.service.SystemDictionaryitemService;
import cn.wolfcode.crm.utils.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/systemDictionaryitem")
public class SystemDictionaryitemController {
    @Autowired
    private SystemDictionaryitemService systemDictionaryitemService;
    @Autowired
    private SystemDictionaryService systemDictionaryService;

    @RequestMapping("/list")
    @RequiresPermissions(value = {"systemDictionaryitemController:list","字典明细列表"},logical = Logical.OR)
    public String list(Model model , @ModelAttribute("qo" )SystemDictionaryitemQueryObiect qo) {
        PageInfo<SystemDictionaryitem> pageInfo = systemDictionaryitemService.query(qo);
        List<SystemDictionary> systemDictionaries = systemDictionaryService.selectAll();

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("systemDictionaries", systemDictionaries);
        return "/systemDictionaryitem/list";
    }

    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = {"systemDictionaryitemController:delete","字典明细删除"},logical = Logical.OR)
    public JsonResult delete(Long id) {
       try {
           systemDictionaryitemService.deleteByPrimaryKey(id);
       }catch (Exception e){
           return new JsonResult(false,"删除失败");

       }
        return new JsonResult(true,"删除成功");



    }
    @RequestMapping("/input")
    @RequiresPermissions(value = {"systemDictionaryitemController:input","字典明细编辑"},logical = Logical.OR)
    public String input(Model model,Long id) {
        if (id != null) {
            SystemDictionaryitem systemDictionaryitem = systemDictionaryitemService.selectByPrimaryKey(id);
            model.addAttribute("systemDictionaryitem",systemDictionaryitem);
        }
        return "/systemDictionaryitem/input";

    }
    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"systemDictionaryitemController:saveOrUpdate","字典明细新增或修改"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(SystemDictionaryitem systemDictionaryitem) {

        try {
           if (systemDictionaryitem.getId()!=null) {
               systemDictionaryitemService.updateByPrimaryKey(systemDictionaryitem);
           }else {
               systemDictionaryitemService.insert(systemDictionaryitem);
           }
            //int i=1/0;
       }catch (Exception e){
           return new JsonResult(false,"保存或者修改失败");
       }
        return new JsonResult(true,"保存或者修改成功");





    }


}
